package com.controller;

import com.Mapper.LandRepeatMapper.LandRepeatMapper;
import com.Mapper.SecondaryMapper.SecondaryMapper;
import com.common.HttpRequest;
import com.common.publicMethod;
import com.param.LandParam.LandDuplicationParam;
import com.param.Users;
import com.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/demo")
public class DemoController {


    @Autowired
    private publicMethod publicMethod;

    @Autowired
    private SecondaryMapper secondaryMapper;

    @Autowired
    private LandRepeatMapper landRepeatMapper;

    public static void main(String[] args) {
        // JVM设置
        // ‐Xms10M ‐Xmx10M ‐XX:+PrintGCDetails ‐XX:+HeapDumpOnOutOfMemoryError ‐XX:HeapDumpPath=D:\jvm.dump
        List<Object> list = new ArrayList<>();
        int i = 0;
        int j = 0;
        while (true) {
            list.add(new Users(i++, UUID.randomUUID().toString()));
            new Users(j--, UUID.randomUUID().toString());
        }
    }

    @Transactional
    @RequestMapping("/OOMTest")
    public void OOMTest() {

        // JVM设置
        // ‐Xms10M ‐Xmx10M ‐XX:+PrintGCDetails ‐XX:+HeapDumpOnOutOfMemoryError ‐XX:HeapDumpPath=D:\jvm.dump
        List<Object> list = new ArrayList<>();
        int i = 0;
        int j = 0;
        while (true) {
            list.add(new Users(i++, UUID.randomUUID().toString()));
            new Users(j--, UUID.randomUUID().toString());
        }
    }


    @RequestMapping("/demo")
    public String demo() {

        log.error("213543");
        return "hello springBoot1234";
    }

    @GetMapping("/test")
    public void test() {

        Object obj = secondaryMapper.selectVersion();
        log.error("213543");
    }


    @GetMapping("/getMessage")
    public void getMessage(@RequestParam("city") String city, HttpServletResponse response) throws IOException {
        String sqlCookie = publicMethod.getSqlCookie();
        ArrayList<String> columnidList = new ArrayList<>();
        columnidList.add("3");
        columnidList.add("4");
        columnidList.add("7");
        columnidList.add("77");
        columnidList.add("133");
        columnidList.add("24");
        columnidList.add("17");
        columnidList.add("98");
        columnidList.add("167");
        columnidList.add("25");
        columnidList.add("12");
        columnidList.add("80");
        columnidList.add("134");
        columnidList.add("263");
        columnidList.add("2");
        columnidList.add("276");


        JSONObject param = new JSONObject();
        ArrayList<LandDuplicationParam> list = new ArrayList<>();
        JSONObject param3 = new JSONObject();
        JSONObject param5 = new JSONObject();
        JSONObject param6 = new JSONObject();
        JSONObject param4 = new JSONObject();
        JSONObject param7 = new JSONObject();
        List<JSONObject> array = new ArrayList<>();
        param3.put("Columnid{}", columnidList);
        param3.put("@column", "Columnid,Value,buildingId");
        param6.put("@column", "checkstate,CreateDataDateTime,InputTime,Operator,landCaption,newcheckstate");
        //   for (int i = 0; i < 2; i++) {


        param.put("@column", "cityCaption,landCaption,idList,repeatGoal");
        List<String> cityList = new ArrayList<>(Arrays.asList(city.split(",")));
        param.put("cityCaption{}", cityList);
        JSONObject param1 = new JSONObject();
      /*      param1.put("page", i);
            param1.put("count", 50000);*/
        // 使用流API将数组转换为List
        param1.put("Land_Main_Shu_Repeat", param);
        JSONObject param2 = new JSONObject();
        param2.put("[]", param1);
        String sr = HttpRequest.sendPostAPI("get", param2.toString(), sqlCookie);
        JSONObject sqlData = JSONObject.fromObject(sr);
        if (sqlData.getBoolean("ok") == true && sqlData.containsKey("[]")) {
            for (Object s1 : sqlData.getJSONArray("[]")) {
                JSONObject details = ((JSONObject) s1).getJSONObject("Land_Main_Shu_Repeat");


                String idList = details.getString("idList");
                List<String> listId = new ArrayList<>(Arrays.asList(idList.split(";")));


                for (String s : listId) {
                    JSONObject object = new JSONObject();
                    object.put("cityCaption", details.getString("cityCaption"));
                    object.put("repeatGoal", details.getString("repeatGoal"));
                    param3.put("buildingId", s);
                    param4.put("Land_Shu", param3);
                    param5.put("[]", param4);
                    object.put("landId", s);
                    String ssr = HttpRequest.sendPostAPI("get", param5.toString(), sqlCookie);
                    JSONObject sqlData1 = JSONObject.fromObject(ssr);
                    if (sqlData1.getBoolean("ok") != true) {
                        log.error("Test error message:[{}]", sqlData1.getString("msg"));
                        log.error("Test error data:[{}]", sqlData1.toString());
                    }
                    if (!sqlData1.containsKey("[]")) {
                        log.error("contains error data:[{}]", sqlData1.toString());
                    } else {
                        for (Object s2 : sqlData1.getJSONArray("[]")) {
                            JSONObject detailss = ((JSONObject) s2).getJSONObject("Land_Shu");
                            if (StringUtils.equals(detailss.getString("Columnid"), "3")) {
                                object.put("region", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "4")) {
                                object.put("plate", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "7")) {
                                object.put("address", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "77")) {
                                object.put("announcementDate", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "133")) {
                                object.put("transactionDate", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "24")) {
                                object.put("transactionSituation", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "17")) {
                                object.put("landAttribute", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "98")) {
                                object.put("announcementNumber", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "167")) {
                                object.put("assignee", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "25")) {
                                object.put("landState", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "12")) {
                                object.put("totallandArea", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "80")) {
                                object.put("transferMethod", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "134")) {
                                object.put("grossTransactionPrice", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "263")) {
                                object.put("landNumber", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "2")) {
                                object.put("source", detailss.get("Value"));
                            }
                            if (StringUtils.equals(detailss.getString("Columnid"), "276")) {
                                object.put("scanSource", detailss.get("Value"));
                            }
                        }
                    }


                    param6.put("id", s);
                    param7.put("Internal_Land_BaseInfo_Main", param6);
                    //   param8.put("[]", param7);
                    String sssr = HttpRequest.sendPostAPI("get", param7.toString(), sqlCookie);
                    JSONObject sqlData2 = JSONObject.fromObject(sssr);
                    JSONObject baseInfoMain = (JSONObject) sqlData2.get("Internal_Land_BaseInfo_Main");
                    if (baseInfoMain != null) {
                        if (baseInfoMain.containsKey("landCaption")) {
                            object.put("landCaption", baseInfoMain.get("landCaption"));
                        }
                        if (baseInfoMain.containsKey("checkstate")) {
                            object.put("checkstate", baseInfoMain.get("checkstate"));
                        }
                        if (baseInfoMain.containsKey("CreateDataDateTime")) {
                            object.put("CreateDataDateTime", baseInfoMain.get("CreateDataDateTime"));
                        }
                        if (baseInfoMain.containsKey("InputTime")) {
                            object.put("InputTime", baseInfoMain.get("InputTime"));
                        }
                        if (baseInfoMain.containsKey("Operator")) {
                            object.put("Operator", baseInfoMain.get("Operator"));
                        }
                        if (baseInfoMain.containsKey("newcheckstate")) {
                            object.put("newcheckstate", baseInfoMain.get("newcheckstate"));
                        }
                    } else {
                        log.error("id error data:[{}]", s);
                    }

                    array.add(object);
                }
            }
            //}

        }
        String[] heads = {"城市", "土地名", "土地id", "去重目标", "操作人", "操作时间", "创建时间", "显示状态", "审核", "区域", "板块", "地址", "公告发布日期", "成交日期", "成交情况", "土地属性", "公告号宗地号", "受让方", "土地现状", "总用地面积", "出让方式", "成交总价", "宗地编号", "信息来源", "扫描来源"};
        log.error("213543");
        OutputStream out = response.getOutputStream();
        XSSFWorkbook workbook = new XSSFWorkbook();
        ExcelUtil.importExcel("土地重复详细信息", heads, array, workbook);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("土地重复数据详情" + ".xlsx", "utf-8"));
        //   response.setContentType("application/x-msdownload");
        //  response.setHeader("Content-Disposition", "attachment; filename= BindDevice.xlsx");
        workbook.write(out);
        if (out != null) {
            out.close();
        }
    }
}



